<?php
class ModelAffiliateSubTransaction extends Model {
	public function getSubTransactions($data = array()) {
		$sql = "SELECT at.* FROM `" . DB_PREFIX . "affiliate_transaction` as at left join `" . DB_PREFIX . "affiliate` as a on a.affiliate_id = at.affiliate_id  WHERE a.pid = '" . (int)$this->affiliate->getId() . "'";
//		echo $sql;
		$sort_data = array(
			'amount',
			'description',
			'date_added'
		);
	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY date_added";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);
	
		return $query->rows;
	}	
		
	public function getTotalSubTransactions() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "affiliate_transaction` as at left join `" . DB_PREFIX . "affiliate` as a on a.affiliate_id = at.affiliate_id WHERE a.pid = '" . (int)$this->affiliate->getId() . "'");
			
		return $query->row['total'];
	}	
			
	public function getSubBalance() {
		$query = $this->db->query("SELECT SUM(agency_amount) AS total FROM `" . DB_PREFIX . "affiliate_transaction` as at left join `" . DB_PREFIX . "affiliate` as a on a.affiliate_id = at.affiliate_id WHERE a.pid = '" . (int)$this->affiliate->getId() . "'");
		
		if ($query->num_rows) {
			return $query->row['total'];
		} else {
			return 0;	
		}
	}

    public function getTransactionsAmount($data = array()) {
        $sql = "SELECT * FROM `" . DB_PREFIX . "order` WHERE affiliate_id = '" . (int)$this->affiliate->getId() . "'";

        $sort_data = array(
            'amount',
            'description',
            'date_added'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY date_added";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getTotalTransactionsAmount() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "order` WHERE affiliate_id = '" . (int)$this->affiliate->getId() . "'");

        return $query->row['total'];
    }
}
?>